<?php
/**
 *
 * @package 
 * @since 1.0
 */
 
class SystemAction extends Action{
	/**
	 * ------------------------------
	 * 预处理
	 * ------------------------------
	 */
	function _initialize(){
		if( !isset($_SESSION['adm_name']) or !isset($_SESSION['isAdmin']) ) $this->redirect('Index/login');
	}
	/**
	 * ------------------------------
	 * 系统基本设置
	 * ------------------------------
	 */
	function sysset(){	
		$list = M('sysconfig')->order('id asc')->select();
		foreach($list as $k=>$v){
		   $data[$v['varname']] = $v['value'];
		}
		$this->assign('pagetitle','系统基本参数设置');
		$this->assign($data);
		$this->display();
	}
	
	function setsys(){
		if (!M()->autoCheckToken($_POST)){
			$this->error('非法操作');
			die();
		}
		unset($_POST['__hash__']);
		foreach ($_POST as $k=>$v) {
			$data['value'] = $v;
			M('sysconfig')->where("varname='".$k."'")->save($data);
		}
	
		$this->success("已保存！");
	}
	







	
	/**
	 * ------------------------------
	 * 首页轮播配置
	 * ------------------------------
	 */

	function jiaodian(){
	
		import("@.ORG.Page");
		$count	= M('pz_jiaodian')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_jiaodian')->order('xu asc,id desc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '首页轮播配置列表');
	
		$this->display();
	 }
	
	function editjiaodian(){
        $id = intval($_GET['id']);
		if($id==''){
		     $this->assign('pagetitle', '添加首页轮播配置');
		}else{
			 $list = M('pz_jiaodian')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改首页轮播配置');
		}
		$this->display('jiaodian_edit');
	}
   
   function dojiaodian(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		
		
		if($_POST['id']){
				if( M('pz_jiaodian')->save($_POST) ){
					$this->assign('jumpUrl', U('System/jiaodian'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
		      if( M('pz_jiaodian')->add($_POST) ){
					$this->assign('jumpUrl', U('System/jiaodian'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function deljiaodian(){
	    $ids = $_POST['ids'];
		$del = M('pz_jiaodian')->where("id IN ($ids)")->delete();
        echo $del?'1':'0';
	}
	/**
	 * ------------------------------
	 * 语言配置
	 * ------------------------------
	 */
	function lang(){
	
		import("@.ORG.Page");
		$count	= M('pz_lang')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_lang')->order('xu asc,id desc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
		foreach($list as $k=>$v){
			if($v['state']==1){
				$list[$k]['state']="应用";
			}else{
				$list[$k]['state']="隐藏";
			}
		}
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '语言配置列表');
	
		$this->display();
	 }
	
	function editlang(){
        $id = intval($_GET['id']);
		if($id==''){
		     $this->assign('pagetitle', '添加语言配置');
		}else{
			 $list = M('pz_lang')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改语言配置');
		}
		$this->display('lang_edit');
	}
   
   function dolang(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		if($_POST['state']===NULL){
			$_POST['state']=1;
		}
		if($_POST['id']){
				if( M('pz_lang')->save($_POST) ){
			  		$pzid=$_POST['id'];
					$pzmodel_menu=M('pz_menu');
					$sql_menu="ALTER TABLE `kbjh_pz_menu` ADD `name".$pzid."` VARCHAR(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ";
					$res_menu = $pzmodel_menu->execute($sql_menu);
					$this->assign('jumpUrl', U('System/lang'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
			  
			  $result=M('pz_lang')->add($_POST);
		      if( $result ){
			  		$pzid=$result;
					$pzmodel_menu=M('pz_menu');
					$sql_menu="ALTER TABLE `kbjh_pz_menu` ADD `name".$pzid."` VARCHAR(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ";
					$res_menu = $pzmodel_menu->execute($sql_menu);
					$this->assign('jumpUrl', U('System/lang'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function dellang(){
	    $ids = $_POST['ids'];
		$del = M('pz_lang')->where("id IN ($ids)")->setField('state','0');
        echo $del?'1':'0';
	}
	/**
	 * ------------------------------
	 * 区域配置
	 * ------------------------------
	 */
	function area(){
	
		import("@.ORG.Page");
		$count	= M('pz_area')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_area')->order('xu asc,id desc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '区域配置列表');
	
		$this->display();
	 }
	
	function editarea(){
        $id = intval($_GET['id']);
		if($id==''){
		     $this->assign('pagetitle', '添加区域配置');
		}else{
			 $list = M('pz_area')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改区域配置');
		}
		$this->display('area_edit');
	}
   
   function doarea(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		
		
		if($_POST['id']){
				if( M('pz_area')->save($_POST) ){
					$this->assign('jumpUrl', U('System/area'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
		      if( M('pz_area')->add($_POST) ){
					$this->assign('jumpUrl', U('System/area'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function delarea(){
	    $ids = $_POST['ids'];
		$del = M('pz_area')->where("id IN ($ids)")->delete();
        echo $del?'1':'0';
	}
	/**
	 * ------------------------------
	 * 广告版面配置
	 * ------------------------------
	 */
	function adway(){
	
		import("@.ORG.Page");
		$count	= M('pz_adway')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_adway')->order('xu asc,id desc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '广告版面配置列表');
	
		$this->display();
	 }
	
	function editadway(){
        $id = intval($_GET['id']);
		if($id==''){
		     $this->assign('pagetitle', '添加广告版面配置');
		}else{
			 $list = M('pz_adway')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改广告版面配置');
		}
		$this->display('adway_edit');
	}
   
   function doadway(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		
		
		if($_POST['id']){
				if( M('pz_adway')->save($_POST) ){
					$this->assign('jumpUrl', U('System/adway'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
		      if( M('pz_adway')->add($_POST) ){
					$this->assign('jumpUrl', U('System/adway'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function deladway(){
	    $ids = $_POST['ids'];
		$del = M('pz_adway')->where("id IN ($ids)")->delete();
        echo $del?'1':'0';
	}
	/**
	 * ------------------------------
	 * 特推主页配置
	 * ------------------------------
	 */
	function main(){
	
		import("@.ORG.Page");
		$count	= M('pz_main')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_main')->order('xu asc,id desc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '特推主页配置列表');
	
		$this->display();
	 }
	
	function editmain(){
        $id = intval($_GET['id']);
		if($id==''){
		     $this->assign('pagetitle', '添加特推主页配置');
		}else{
			 $list = M('pz_main')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改特推主页配置');
		}
		$this->display('main_edit');
	}
   
   function domain(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		
		
		if($_POST['id']){
				if( M('pz_main')->save($_POST) ){
					$this->assign('jumpUrl', U('System/main'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
		      if( M('pz_main')->add($_POST) ){
					$this->assign('jumpUrl', U('System/main'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function delmain(){
	    $ids = $_POST['ids'];
		$del = M('pz_main')->where("id IN ($ids)")->delete();
        echo $del?'1':'0';
	}
	/**
	 * ------------------------------
	 * 分类配置
	 * ------------------------------
	 */
	function menu(){
	
		import("@.ORG.Page");
		$count	= M('pz_menu')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$list = M('pz_menu')->order('xu asc,id asc')->limit($Page->firstRow.','.$Page->listRows)
						     ->select();
		foreach($list as $k=>$v){
			if($v['mid']==0){
				$list[$k]['mname']="顶级分类";
			}else{
				$mname=M('pz_menu')->find($v['mid']);
				$list[$k]['mname']=$mname['name'];
			}
		}
	    $this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('pagetitle', '分类配置列表');
	
		$this->display();
	 }
	
	function editmenu(){
        $id = intval($_GET['id']);
		$topmenu=M('pz_menu')->where("mid=0")->order('xu asc,id asc')->select();
	    $this->assign('topmenu', $topmenu);
		$lang=M('pz_lang')->where("state=1")->order('xu asc,id asc')->select();
	    $this->assign('lang', $lang);
		if($id==''){
		     $this->assign('pagetitle', '添加分类配置');
		}else{
			 $list = M('pz_menu')->find($id);
			 $this->assign($list);
			 $this->assign('type', 'edit');
			 $this->assign('pagetitle', '修改分类配置');
		}
		//var_dump($list);
		$this->display('menu_edit');
	}
   
   function domenu(){
	   
   	    header("Content-Type:text/html; charset=utf-8");
		if( !M()->autoCheckToken($_POST) ) $this->error('禁止站外提交！');
		unset($_POST['__hash__']);
		foreach ($_POST as $k => $v) {
			if($v == '') unset($_POST[$k]);
		}
		
		
		if($_POST['id']){
				if( M('pz_menu')->save($_POST) ){
					$this->assign('jumpUrl', U('System/menu'));
					$this->success('修改成功！');
				}else{
					$this->error('没有任何改动　或　修改失败！');
				}
		}else{
		      if( M('pz_menu')->add($_POST) ){
					$this->assign('jumpUrl', U('System/menu'));
					$this->success('添加成功！');
			  }else{
					$this->error('添加失败！');
			  }
		}
	}
	
	function delmenu(){
	    $ids = $_POST['ids'];
		$del = M('pz_menu')->where("id IN ($ids)")->delete();
        echo $del?'1':'0';
	}
 
}